package org.yanse.structures.linkedList;

public class JosephusProblem {
    public static int survivor(int n, int m) {
        if (n <= 0) {
            return -1;
        }

        int[] arrays = new int[n];
        for (int i = 1; i <= n; i++) {
            arrays[i - 1] = i;
        }

        CircularLinkedList list = new CircularLinkedList(arrays);
        int index = m - 1;
        while (list.size() > 1) {
            if (index >= list.size()) {
                index %= list.size();
            }

            list.remove(index);
            index += m - 1;
        }

        return list.get(0);
    }
}
